package fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.projectmoudle_two.R;


/**
 * Created by eyesee on 2017/5/9.
 */

/**STATE_COLLAPSED： 默认的折叠状态， bottom sheets只在底部显示一部分布局。显示高度可以通过 app:behavior_peekHeight 设置（默认是0）
 STATE_DRAGGING ： 过渡状态，此时用户正在向上或者向下拖动bottom sheet
 STATE_SETTLING: 视图从脱离手指自由滑动到最终停下的这一小段时间
 STATE_EXPANDED： bottom sheet 处于完全展开的状态：当bottom sheet的高度低于CoordinatorLayout容器时，整个bottom sheet都可见；或者CoordinatorLayout容器已经被bottom sheet填满。
 STATE_HIDDEN ： 默认无此状态（可通过app:behavior_hideable 启用此状态），启用后用户将能通过向下滑动完全隐藏 bottom sheet*/

public class BottomSheetFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "BottomSheetFragment";

    private View mView;
    private BottomSheetBehavior<View> mBehavior;
    private TextView mBottomSheetHeading;
    private Button mExpandBottomSheet;
    private Button mCollapseBottomSheet;
    private Button mHideBottomSheet;
    private Button mShowBottomSheetDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(getActivity(), R.layout.fragment_buttomsheet_main, null);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(mView);
        initListener();
        return mView;
    }

    private void initListener() {
        mExpandBottomSheet.setOnClickListener(this);
        mCollapseBottomSheet.setOnClickListener(this);
        mHideBottomSheet.setOnClickListener(this);
        mShowBottomSheetDialog.setOnClickListener(this);

        mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(View bottomSheet, int newState) {
                if (newState==BottomSheetBehavior.STATE_EXPANDED){
                    mBottomSheetHeading.setText(getString(R.string.text_collapse_me));
                }else {
                    mBottomSheetHeading.setText(getString(R.string.text_expanded_me));
                }

                switch (newState){
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        Log.e(TAG, "STATE_COLLAPSED: " );
                        break;
                    case BottomSheetBehavior.STATE_DRAGGING:
                        Log.e(TAG, "STATE_DRAGGING: ");
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED:
                        Log.e(TAG, "STATE_EXPANDED");
                        break;
                    case BottomSheetBehavior.STATE_HIDDEN:
                        Log.e(TAG, "STATE_HIDDEN");
                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        Log.e(TAG, "STATE_SETTLING");
                        break;
                }
            }

            @Override
            public void onSlide(View bottomSheet, float slideOffset) {

            }
        });
    }

    private void initView(View view) {
        mBehavior = BottomSheetBehavior.from(view.findViewById(R.id.bottomsheetlayout));
        mBottomSheetHeading = ((TextView) view.findViewById(R.id.bottomsheetHeading));

        mExpandBottomSheet = ((Button) view.findViewById(R.id.expand_buttom_sheet));
        mCollapseBottomSheet = ((Button) view.findViewById(R.id.collapse_buttom_sheet));
        mHideBottomSheet = ((Button) view.findViewById(R.id.hide_buttom_sheet));
        mShowBottomSheetDialog = ((Button) view.findViewById(R.id.show_buttom_sheet_dialog));


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.expand_buttom_sheet:
                mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                break;
            case R.id.collapse_buttom_sheet:
                mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                break;
            case R.id.hide_buttom_sheet:
                mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                break;
            case R.id.show_buttom_sheet_dialog:
                Toast.makeText(getActivity(), "showDialog", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
